<?php

namespace app\common\model;

use app\store\model\Category as CategoryModel;
use think\Cache;

/**
 * 课程模型
 * Class Lesson
 * @package app\common\model
 */
class Lesson extends BaseModel
{
    protected $name = 'lesson';

    /**
     * 关联分类表
     * @return \think\model\relation\BelongsTo
     */
    public function category()
    {
        return $this->belongsTo('Category');
    }
    /**
     * 关联内容表
     * @return \think\model\relation\BelongsTo
     */
    public function details()
    {
        return $this->hasMany('LessonDetails','lesson_id','lesson_id');
    }
    /**
     * 所有内容
     * @return mixed
     */
    public function getALL($param)
    {
        // 商品列表获取条件
        $params = array_merge([
            'category_id' => 0,     // 分类id
            'listRows' => 50,       // 每页数量
        ], $param);
        // 筛选条件
        $filter = [];
        $params['category_id'] > 0 && $filter['category_id'] = ['IN', Category::getSubCategoryId($params['category_id'])];
        //$params['status'] > 0 && $filter['status'] = $params['status'];
        // 排序规则
        $sort = [];
        // 执行查询
        $list = $this
            ->with(['category'])
            ->where('is_delete', '=', 0)
            ->where($filter)
            ->order('sort,lesson_id desc')
            ->paginate($params['listRows'], false, [
                'query' => \request()->request()
            ]);
        // 整理列表数据并返回
        $dataSource = &$list;
        foreach ($dataSource as &$cat) {
            // 所属分类名称
            $cat['cate_all'] = CategoryModel::getParentCategory($cat['category_id']);
            $cat['cate_name'] = '';
            foreach ($cat['cate_all'] as $k=>$v){
                $cat['cate_name'] = $k==0?$cat['cate_name'].$v['name']:$cat['cate_name']."<br>　　└".$v['name'];
            }
        }
        return $list;
    }



}
